﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace RuleCode.Common
{
    
    public class TableRelation : RelationObject
    {
        private DataRelation dataRelation;

        #region 构造函数
        public TableRelation() { }
        public TableRelation(string relationName, DataColumn[] parentColumns, DataColumn[] childColumns)
        {
            this.dataRelation = new DataRelation(relationName, parentColumns, childColumns);
        }
        #endregion

        #region 属性
        /// <summary>
        /// 获取或者设置父子关系DataRelation
        /// </summary>
        public DataRelation DataRelation
        {
            get { return this.dataRelation; }
            set { this.dataRelation = value; }
        }

        /// <summary>
        /// 获取主表的主键列名集合
        /// </summary>
        public List<string> ParentColumnCaptions
        {
            get
            {
                List<string> captionString = new List<string>();
                foreach (DataColumn col in this.ParentColumns)
                    captionString.Add(col.Caption);
                return captionString;
            }
        }

        /// <summary>
        /// 获取子表的主键列名集合
        /// </summary>
        public List<string> ChildColumnCaptions
        {
            get
            {
                List<string> captionString = new List<string>();
                foreach (DataColumn col in this.ChildColumns)
                    captionString.Add(col.Caption);
                return captionString;
            }
        }


        //这个属性暂时测试没通过
        public Table ChildTable
        {
            get
            {
                foreach (Table table in SessionFactory.Instance.TableList)
                {
                    if (table.MyTable == this.dataRelation.ChildTable)
                        return table;
                }
                return null;
            }
        }

        //这个属性暂时测试没通过
        public Table ParentTable
        {
            get
            {
                foreach (Table table in SessionFactory.Instance.TableList)
                {
                    if (table.MyTable.Equals(this.dataRelation.ParentTable))
                        return table;
                }
                return null;
            }
        }



        public DataColumn[] ParentColumns
        { get { return this.dataRelation.ParentColumns; } }
        public DataColumn[] ChildColumns
        { get { return this.dataRelation.ChildColumns; } }
        public string RelationName
        { get { return this.dataRelation.RelationName; } }
        public DataSet DataSet
        { get { return this.dataRelation.DataSet; } }
        #endregion

    }
    
}
